package hku.cs.lunuo.tools;

public class MD5 {
	public static String getMD5(byte[] source) {
		String s = null;
		char hexDigits[] = { 
		'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
				'e', 'f' };
		try {
			java.security.MessageDigest md = java.security.MessageDigest
					.getInstance("MD5");
			md.update(source);
			byte tmp[] = md.digest(); // MD5 result is a 128 bit long (LongInteger)
			// can present with 16 byte
			char str[] = new char[16 * 2]; // every byte use 2 char 16 hex
			int k = 0; 
			for (int i = 0; i < 16; i++) { 
				byte byte0 = tmp[i]; // the i th byte
				str[k++] = hexDigits[byte0 >>> 4 & 0xf]; // the high 4bit
				
				str[k++] = hexDigits[byte0 & 0xf]; // the low 4 bit
			}
			s = new String(str); // translate to String

		} catch (Exception e) {
			e.printStackTrace();
		}
		return s;
	}
}